//
//  Serial_RemoteAppDelegate.m
//
//	Serial Remote for Mac - Control mac actions via serial commands
//	Copyright (C) 2010  Jeremiah McConnell
//
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#import "Serial_RemoteAppDelegate.h"
#import "MBPreferencesController.h"
#import "PreferencesDeviceViewController.h"
#import "PreferencesBindingsViewController.h"
#import "PreferencesGeneralViewController.h"
#import "MonitorWindowController.h"
#import "SerialPortSelectionData.h"
#import "BindingsData.h"
#import "SerialPortReader.h"
#import "SRKeyCodeTransformer.h"

MonitorWindowController *monitorWindow = nil;

@implementation Serial_RemoteAppDelegate

#pragma mark -
#pragma mark Class Methods

+ (void)initialize
{
	// Set the default user preferences
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	NSString *defaultsPath = [[NSBundle mainBundle] pathForResource:@"UserDefaults" ofType:@"plist"];
	NSDictionary *defaultsDict = [NSDictionary dictionaryWithContentsOfFile:defaultsPath];
	[userDefaults registerDefaults:defaultsDict];
	
	// Restore the bindings data from persistent store
	NSArray *storedBindings = [userDefaults arrayForKey:@"bindings"];
	[[BindingsData sharedBindingsData] addObjectsFromArray:storedBindings];
	
	// Register value transformers
	SRKeyCodeTransformer *keyCodeTransformer = [[SRKeyCodeTransformer alloc] init];
	[NSValueTransformer setValueTransformer:keyCodeTransformer forName:@"StringToKeyCodeTransformer"];
}

#pragma mark -
#pragma mark Applicaion Cleanup

- (void)applicationWillTerminate:(NSNotification *)aNotification
{
	// If there's a reader running, stop it
	SerialPortReader *reader = [SerialPortReader sharedSerialPortReader];
	[reader stop];
	
	// Save bindings to persistent store
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults setObject:[BindingsData sharedBindingsData] forKey:@"bindings"];
}

#pragma mark -
#pragma mark Application Startup

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	
	// Setup the system status bar menu
	NSStatusBar *systemStatusBar = [NSStatusBar systemStatusBar];
	statusItem = [systemStatusBar statusItemWithLength:NSVariableStatusItemLength];
	//[statusItem setTitle:@"Remote"];
	
	NSImage *icon = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"StatusBarIcon" ofType:@"png"]];
	NSImage *iconSelected = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"StatusBarIconSelected" ofType:@"png"]];
	
	[statusItem setImage:icon];
	[statusItem setAlternateImage:iconSelected];
	[statusItem setHighlightMode:YES];
	[statusItem setMenu:statusBarMenu];
	
	
	// Application startup here
	SerialPortReader *reader = [SerialPortReader sharedSerialPortReader];
	[reader start];
}

#pragma mark -
#pragma mark IBAction Methods

// Show the preferences dialog
- (IBAction)showPreferences:(id)sender
{
	if ([[MBPreferencesController sharedController] modules] == nil) {
		PreferencesBindingsViewController *bindings = [[PreferencesBindingsViewController alloc] initWithNibName:@"PreferencesBindings" bundle:nil];
		PreferencesDeviceViewController *device = [[PreferencesDeviceViewController alloc] initWithNibName:@"PreferencesDevice" bundle:nil];
		PreferencesGeneralViewController *general = [[PreferencesGeneralViewController alloc] initWithNibName:@"PreferencesGeneral" bundle:nil];
		[[MBPreferencesController sharedController] setModules:[NSArray arrayWithObjects:general, bindings, device, nil]];
	}
	
	[[MBPreferencesController sharedController] showWindow:sender];
}

- (IBAction)showSerialMonitor:(id)sender
{
	if (monitorWindow == nil) {
		monitorWindow = [[MonitorWindowController alloc] initWithWindowNibName:@"MonitorWindow"];
	}

	[monitorWindow showWindow:sender];
}

@end
